package com.dianrui.firemenmonitor.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dianrui.firemenmonitor.R;
import com.dianrui.firemenmonitor.bean.AttendaceBean;
import com.dianrui.firemenmonitor.util.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AttendaceListAdapter extends BaseAdapter {

    private List<AttendaceBean> attendaceBeanList;
    private Context context;


    public AttendaceListAdapter(Context context, List<AttendaceBean> mainArticlesList) {
        this.context = context;
        this.attendaceBeanList = mainArticlesList;
    }

    public void refresh(List<AttendaceBean> list) {
        this.attendaceBeanList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (attendaceBeanList.size()==0) {
            return 0;
        }
        return attendaceBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return attendaceBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_attendce, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (attendaceBeanList != null && attendaceBeanList.size()>0) {
            AttendaceBean attendaceBean=attendaceBeanList.get(position);
           if (attendaceBean.status_on.equals("0")) {
                holder.noneAtendancelayout.setVisibility(View.GONE);
                holder.all_layout.setVisibility(View.VISIBLE);
                holder.content.setText(attendaceBean.cixu+" "+attendaceBean.onehitcard);
               if (StringUtils.isEmpty(attendaceBean.address)) {
                   holder.address.setText("暂无位置信息");
               }else {
                   holder.address.setText(attendaceBean.address);
               }

                if (attendaceBean.type_one.equals("1")) {
                    holder.status.setText("迟到");
                    holder.status.setVisibility(View.VISIBLE);
                }else if (attendaceBean.type_one.equals("2")) {
                    holder.status.setText("早退");
                    holder.status.setVisibility(View.VISIBLE);
                }else if (attendaceBean.type_one.equals("3")) {
                    holder.status.setText("出界");
                    holder.status.setVisibility(View.VISIBLE);
                }else if (attendaceBean.type_one.equals("4")) {
                    holder.status.setText("迟到出界");
                    holder.status.setVisibility(View.VISIBLE);
                }else if (attendaceBean.type_one.equals("5")) {
                    holder.status.setText("早退出界");
                    holder.status.setVisibility(View.VISIBLE);
                }else if (attendaceBean.type_one.equals("6")) {
                    holder.status.setText("未打卡");
                    holder.status.setVisibility(View.VISIBLE);
                }
            }else  if (attendaceBean.status_on.equals("1")) {
                holder.noneAtendancelayout.setVisibility(View.VISIBLE);
                holder.no_attendce.setText(attendaceBean.status);
                holder.all_layout.setVisibility(View.GONE);

            }
        }

        return convertView;
    }


    class ViewHolder {

        @BindView(R.id.tvDot)
        TextView tvDot;
        @BindView(R.id.rlTimeline)
        RelativeLayout rlTimeline;
        @BindView(R.id.content)
        TextView content;
        @BindView(R.id.status)
        Button status;
        @BindView(R.id.address_layout)
        LinearLayout addressLayout;
        @BindView(R.id.item_attendce_img)
        ImageView itemAttendceImg;
        @BindView(R.id.address)
        TextView address;
        @BindView(R.id.no_attendce)
        TextView no_attendce;
        @BindView(R.id.rlCenter)
        LinearLayout rlCenter;
        @BindView(R.id.all_layout)
        LinearLayout all_layout;
        @BindView(R.id.none_attendancelayout)
        LinearLayout noneAtendancelayout;



        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
